package cn.maihe.elg.operation.utils;

import org.apache.commons.lang3.StringUtils;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Base64;

/**
 * @author JHWei
 * @date 2018/7/23
 */
public class Base64Util {

	public static final String DEFAULT_CHARSET = "UTF-8";

	public static String img_base64(String path) {
		/**
		 *  对path进行判断，如果是本地文件就二进制读取并base64编码，如果是url,则返回
		 */
		String imgBase64="";
		if (path.startsWith("http")){
			imgBase64 = path;
		}else {
			try {
				File file = new File(path);
				byte[] content = new byte[(int) file.length()];
				FileInputStream finputstream = new FileInputStream(file);
				finputstream.read(content);
				finputstream.close();
				imgBase64 = new String(org.apache.commons.codec.binary.Base64.encodeBase64(content));
			} catch (IOException e) {
				e.printStackTrace();
				return imgBase64;
			}
		}

		return imgBase64;
	}

	/**
	 * base64加密
	 * @param src
	 * @return
	 */
	public static byte[] encode(String src) {
		return encode(src, DEFAULT_CHARSET);
	}

	/**
	 * base64加密
	 * @param src
	 * @param charset
	 * @return
	 */
	public static byte[] encode(String src, String charset) {
		byte[] res = null;
		try {
			if (StringUtils.isBlank(charset)) {
				charset = DEFAULT_CHARSET;
			}

			byte[] b = src.getBytes(charset);

			res = encode(b);
		} catch(Exception ex) {
			ex.printStackTrace();
			throw new RuntimeException("base64加密异常");
		}
		return res;
	}

	/**
	 * base64加密
	 * @param src
	 * @return
	 */
	public static byte[] encode(byte[] src) {
		return Base64.getEncoder().encode(src);
	}


	/**
	 * base64加密
	 * @param src
	 * @return
	 */
	public static String encode2String(byte[] src) {
		return Base64.getEncoder().encodeToString(src);
	}


	/**
	 * base64加密
	 * @param src
	 * @return
	 */
	public static byte[] encode2Byte(String src) {
		return Base64.getDecoder().decode(src);
	}

	/**
	 * base64解密
	 * @param src
	 * @return
	 */
	public static byte[] decode(String src) {
		return decode(src, DEFAULT_CHARSET);
	}

	public static byte[] mimeDecoder(String src){
		return mimeDecoder(src, DEFAULT_CHARSET);
	}

	/**
	 * base64解密
	 * @param src
	 * @param charset
	 * @return
	 */
	public static byte[] mimeDecoder(String src, String charset) {
		byte[] res = null;
		try {
			if (StringUtils.isBlank(charset)) {
				charset = DEFAULT_CHARSET;
			}

			byte[] b = src.getBytes(charset);

			res = mimeDecode(b);
		} catch (Exception ex) {
			ex.printStackTrace();
			throw new RuntimeException("base64解密异常");
		}
		return res;
	}

	/**
	 * base64解密
	 * @param src
	 * @param charset
	 * @return
	 */
	public static byte[] decode(String src, String charset) {
		byte[] res = null;
		try {
			if (StringUtils.isBlank(charset)) {
				charset = DEFAULT_CHARSET;
			}

			byte[] b = src.getBytes(charset);

			res = decode(b);
		} catch (Exception ex) {
			ex.printStackTrace();
			throw new RuntimeException("base64解密异常");
		}
		return res;
	}

	/**
	 * base64解密
	 * @param src
	 * @return
	 */
	public static byte[] decode(byte[] src) {
		return Base64.getDecoder().decode(src);
	}

	/**
	 * base64解密 有换行符号的，设置成MINE的
	 * @param src
	 * @return
	 */
	public static byte[] mimeDecode(byte[] src) {
		return Base64.getMimeDecoder().decode(src);
	}
}
